package com.example.sinoyd.frameapplication.ProjectCodes.Fragment

import android.app.Fragment
import android.os.Bundle
import android.os.Looper
import android.view.View
import com.example.shenchuanjiang.kotlin1013test.CommonSelector
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.gson
import com.example.sinoyd.frameapplication.KotlinFrame.Frame.Uitl.Networkrequestmodel
import com.example.sinoyd.frameapplication.KotlinFrame.MainActivity
import com.example.sinoyd.frameapplication.KotlinFrame.Uitl.showdialog
import com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetPointInfo
import com.example.sinoyd.frameapplication.R
import com.sinoyd.Code.Until.HttpListener
import com.sinoyd.Code.Until.Networkrequestaddress
import com.sinoyd.Code.Until.SharedPreferencesFactory
import com.sinoyd.Code.Until.ShowLog4okhttp
import kotlinx.android.synthetic.main.include_title_layout.*
import okhttp3.Call
import okhttp3.Response
import org.jetbrains.anko.*

/**
 * 作者： scj
 * 创建时间： 2018/5/21
 * 版权： 江苏远大信息股份有限公司
 * 描述： com.example.sinoyd.frameapplication.KotlinFrame.Fragment
 */

abstract class BaseFragment : Fragment(), HttpListener {

    var responsestr = ""

    var portid = ""


    var getPointInfo: GetPointInfo = GetPointInfo()

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        when (SharedPreferencesFactory.getdata(act, "OS")) {
            "air" -> {
                //是否存在站点信息类
                if (SharedPreferencesFactory.getclass(act, "PointInfoAir", GetPointInfo.PointInfoBean::class.java) == null) {
                    GetPointInfoforAir()
                } else {
                    getPointInfo = SharedPreferencesFactory.getclass(act, "PointInfoAir", GetPointInfo::class.java)!!
                    portid = getPointInfo.pointInfo[0].id
                }
                iv_right_two.setImageDrawable(act.resources.getDrawable(R.drawable.more_change_system))

            }
            "water" -> {
                //是否存在站点信息类
                if (SharedPreferencesFactory.getclass(act, "PointInfoWater", GetPointInfo.PointInfoBean::class.java) == null) {
                    GetPointInfoforWater()
                } else {
                    getPointInfo = SharedPreferencesFactory.getclass(act, "PointInfoWater", GetPointInfo::class.java)!!
                    portid = getPointInfo.pointInfo[0].id
                }
                iv_right_two.setImageDrawable(act.resources.getDrawable(R.drawable.change_station_icon))
            }
        }

        iv_right_two.onClick {
            when (SharedPreferencesFactory.getdata(act, "OS")) {
                "air" -> {
                    SharedPreferencesFactory.savedata(act, "OS", "water")
                    act.toast("切换成水系统")
                    act.startActivity<MainActivity>()
                    act.finish()
                }
                "water" -> {
                    SharedPreferencesFactory.savedata(act, "OS", "air")
                    act.toast("切换成气系统")
                    act.startActivity<MainActivity>()
                    act.finish()
                }
            }
        }

        iv_left_one.onClick {
            var comm = CommonSelector<GetPointInfo.PointInfoBean>(act, getPointInfo.pointInfo as ArrayList<GetPointInfo.PointInfoBean>, object : CommonSelector.OnSelectClickListener {
                override fun onCommonItemSelect(postions: Int) {
                    portid = getPointInfo.pointInfo[postions].id
                    selectPortCallBack()
                }
            })
            comm.showPop()
        }

    }


    override fun requestSuccess(response: Response, TAG: String) {
        this.runOnUiThread {
            showdialog(act, "loadsuccess")
        }
        responsestr = response.body().string()
        ShowLog4okhttp("scj").printinfo(response).printJson(responsestr)

        when (TAG) {
            "PointInfoWater" -> {
                getPointInfo = gson.fromJson(responsestr, GetPointInfo::class.java)
                if (getPointInfo.pointInfo.size > 0) {
                    //存储
                    SharedPreferencesFactory.saveclass(act, "PointInfoWater", responsestr)
                    portid = getPointInfo.pointInfo[0].id
                }
            }
            "PointInfoAir" -> {
                getPointInfo = gson.fromJson(responsestr, GetPointInfo::class.java)
                if (getPointInfo.pointInfo.size > 0) {
                    //存储
                    SharedPreferencesFactory.saveclass(act, "PointInfoAir", responsestr)
                    portid = getPointInfo.pointInfo[0].id
                }
            }
        }
    }

    override fun requestFailed(response: Response) {
        this.runOnUiThread {
            showdialog(act, "loadfail")
        }
        responsestr = response.body().string()
        ShowLog4okhttp("scj").printinfo(response).printJson(responsestr)
        Looper.prepare()
        toast("请求失败")
        Looper.loop()
    }

    override fun onFailure(call: Call) {
        this.runOnUiThread {
            showdialog(act, "loadfail")
        }
        ShowLog4okhttp("scj").printinfo(call)
        Looper.prepare()
        toast("请求出错")
        Looper.loop()
    }

    override fun onDestroy() {
        super.onDestroy()
        showdialog(act, "loaddismiss")
    }


    abstract fun selectPortCallBack()

    //气的站点信息
    fun GetPointInfoforAir() {
        showdialog(act, "loadshow")
        var ntrequest = Networkrequestmodel()
        ntrequest.setMethod(Networkrequestmodel.GETREQUEST)
                .settag("PointInfoAir")
                .seturl(Networkrequestaddress.URL_GetPointInfo)
                .addparam("sysType", "air")
                .start(this)
    }

    //水的站点信息
    fun GetPointInfoforWater() {
        showdialog(act, "loadshow")
        var ntrequest = Networkrequestmodel()
        ntrequest.setMethod(Networkrequestmodel.GETREQUEST)
                .settag("PointInfoWater")
                .seturl(Networkrequestaddress.URL_GetPointInfo)
                .addparam("sysType", "water")
                .start(this)
    }

}